#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time       :   16:22
# Author     : Y4
# Site       : 
# File       : 005 文件操作总结.py
# Software   : PyCharm
import os


# 删除文件夹







# 复制文件夹
path = os.path.dirname(__file__)                # 当前文件夹
sourcePath = os.path.join(path,'copy')          # 源文件夹
targetPath = os.path.join(path,'copy2')         # 目标文件夹


# 创建目标文件夹
has = os.path.exists(targetPath)
if not has:
    os.mkdir(targetPath)


# 复制文件方法
def copy(sourcePath,targetPath):
    if os.path.isdir(sourcePath) and os.path.isdir(targetPath):                 # 如果确定是一个文件夹
        fileList = os.listdir(sourcePath)                                       # 文件夹下的文件列表

        for file in fileList:
            path  = os.path.join(sourcePath,file)                   # 读取文件的绝对路径


            # 判断是否是文件夹
            if os.path.isdir(path):
                # 是文件夹

                targetPath1 = os.path.join(targetPath,file)         # 目标文件夹内部创建文件夹
                os.mkdir(targetPath1)

                copy(path,targetPath1)

            else:
                # 不是文件夹
                with open(path,'rb') as stream:                          # 读取文件内容
                    container = stream.read()

                    path1 = os.path.join(targetPath,file)               # 写入文件的绝对路径
                    with open(path1,'wb') as wstream:                    # 写入文件内容
                        wstream.write(container)

        else:
            print("复制完毕")


# 调用方法
copy(sourcePath,targetPath)

